package JUC.多线程;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author CKC
 * @DateTime 2021/6/12 10:59
 */
public class TestSchedule {
    
    //如何让每周四18：00定时执行任务？
    public static void main(String[] args) {
        
        //initialDelay 代表当前时间和周四的时间差
        //period   一周的间隔时间
        
        long period = 1000 * 60 * 60 * 24 * 7;
        LocalDateTime now = LocalDateTime.now();    /*获取当前时间*/
        LocalDateTime time = now.withHour(18).withMinute(0).withSecond(0).withNano(0).with(DayOfWeek.THURSDAY);
        if (now.compareTo(time) > 0) {/*如果  当前时间>本周周四，必须找到下周周四*/
            time = time.plusWeeks(1);
        }
        /*Duration  对时间进行运算的类*/
        long initialDelay = Duration.between(now, time).toMillis();
    
    
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
        pool.scheduleAtFixedRate(() -> {
            System.out.println("running.....");
        }, initialDelay, period, TimeUnit.MILLISECONDS);
    }
    
    
}
